<?php
include('common_include.php');
include('validateSession.php');

/* Information for add operation */
$location = $_POST["location"];
$address = $_POST["address"];
$telephone = $_POST["telephone"];
/* end add operation */

$operation = $_POST["operation"];
$deleteID = $_POST["deleteID"];
$outletArr = $_POST["outlet"];

//var_dump($_POST);
//var_dump($outletArr);

//$operation = 'Delete';
//$deleteID = '4';


$dao = new SQLitePersistence();

if ($operation == 'Add' && $location != '' && $address != '') {
	try {
		$outletObj = new Outlet();
		$outletObj->setOutletLocation($location);
		$outletObj->setOutletAddress($address);
		$outletObj->setOutletTelephone($telephone);
		$dao -> saveNewOutlet($outletObj);
		header("Location: outlet_update.php");
	} catch (Exception $e) {
		header("Location: outlet_update.php?error=Save Outlet Failed.");
	}
} else if ($operation == 'Delete' && $deleteID != '') {
	try {
		$dao -> deleteOutlet($deleteID);
		header("Location: outlet_update.php");
	} catch (Exception $e) {
		header("Location: outlet_update.php?error=Delete Outlet Failed");
	}
} else if ($operation == 'Update' && sizeof($outletArr) > 0) {
	try {
		for ($i=0; $i < sizeof($outletArr); $i++) {
			$outletObj = new Outlet();			
			$outletObj->setOutletId($outletArr[$i]['outletID']);
			$outletObj->setOutletLocation($outletArr[$i]['location']);
			$outletObj->setOutletAddress($outletArr[$i]['address']);
			$outletObj->setOutletTelephone($outletArr[$i]['telephone']);			
			$outletObj->setOutletSeq($outletArr[$i]['outletSeq']);
			$dao -> updateOutletObj($outletObj);
			unset($outletObj);
		}
		header("Location: outlet_update.php");
	} catch (Exception $e) {
		header("Location: outlet_update.php?error=Update Outlet Failed");
	}
} else {
	Logger::logInfo("Javascript Injection disabled");
}

unset($dao);

?>